#include "form_chagepassword.h"
#include "ui_form_chagepassword.h"
#include "pub_var.h"
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>

Form_chagePassword::Form_chagePassword(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_chagePassword)
{
    ui->setupUi(this);
    qrm = new QSqlQuery(pub::db_m);
    qDebug()<<"修改当前用户口令"<<pub::usr_code;
    QSettings setIni("clin.ini", QSettings::IniFormat);
    //恢复当前窗口位置
    QRect rct = setIni.value("windowState/changepswd").toRect();
    this->setGeometry(rct);
    //this->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
}

Form_chagePassword::~Form_chagePassword()
{
    delete ui;
}

void Form_chagePassword::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/changepswd", QVariant(this->geometry()));

}

void Form_chagePassword::on_pbExit_clicked()
{
    this->close();
}

void Form_chagePassword::on_pbSave_clicked()
{
    QString cur = ui->leOldt->text().trimmed();
    QString new1 = ui->leNew1->text().trimmed();
    QString new2 = ui->leNew2->text().trimmed();

    QString sq;
    sq = "select e.empl_code, e.empl_name, e.dept_code, e.pswd "
         " from com_employee e where  e.empl_code = '" +  pub::usr_code +
         "' and pswd = md5('" + cur + "')";
    qDebug()<<sq;

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"error",qrm->lastError().text());
        qDebug()<<qrm->lastError().text();
        return;
    }
    if(!qrm->next()){
        QMessageBox::information(this,"错误","当前口令输入不正确！");
        qDebug()<<"当前口令输入不正确！";
        return;
    }
    if(new1 != new2){
        QMessageBox::information(this,"错误","新口令输入不一致！");
        qDebug()<<"新口令输入不一致！";
        return;
    }
    if(new1.isEmpty()){
        QMessageBox::information(this,"错误","新口令不可以为空白！");
        qDebug()<<"新口令为空白！";
        return;
    }
    sq = "update com_employee set pswd = md5('" + new1 + "') "
         "where empl_code = '" +  pub::usr_code + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"error",qrm->lastError().text());
        qDebug()<<qrm->lastError().text();
        return;
    }
    qDebug()<<qrm->lastQuery();
    QMessageBox::information(this,"注意","口令修改完成！");
    qDebug()<<"口令修改完成";
}
